#include<iostream>
using namespace std;
int pan[12][12];
int mx[]={-2,-2,-1,-1,1,1,2,2};
int my[]={-1,1,2,-2,2,-2,1,-1};
int n,ans;
int x,y,i;
int main()
{
    scanf("%d",&n);
    while (n--)
    {
        ans=8;
        scanf("\n%c%c",&x,&y);
        x -= 'a' -2;
        y -= '1' -2;
        for ( i = 0; i < 8; i++){
            if(x+mx[i]>=10 
            ||x+mx[i]<=1
            ||y+my[i]>=10
            ||y+my[i]<=1) ans--;
        }
        printf("%d\n",ans);
    }
    
    return 0;
}
